<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="queryParams.userId" placeholder="请输入用户ID" clearable style="width: 200px" @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="订单状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="选择订单状态" clearable style="width: 200px">
            <el-option label="待处理" :value="0" />
            <el-option label="已完成" :value="1" />
            <el-option label="已取消" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
  
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="Plus" @click="handleAdd">新增订单</el-button>
        </el-col>
      </el-row>
  
      <el-table v-if="refreshTable" v-loading="loading" :data="orderList" row-key="id">
        <el-table-column prop="userId" label="用户ID" width="150"></el-table-column>
        <el-table-column prop="totalAmount" label="总金额" width="150">
          <template #default="scope">
            <span>{{ scope.row.totalAmount}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="orderDate" label="订单日期" width="200"></el-table-column>
        <el-table-column prop="status" label="订单状态" width="150">
          <template #default="scope">
            <span>{{ getOrderStatusLabel(scope.row.status) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="paymentMethod" label="支付方式" width="150">
          <template #default="scope">
            <span>{{ getPaymentMethodLabel(scope.row.paymentMethod) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="shippingAddress" label="送货地址" width="200"></el-table-column>
        <el-table-column prop="trackingNumber" label="物流追踪号" width="150"></el-table-column>
        <el-table-column prop="discountCode" label="折扣码" width="150"></el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)">修改</el-button>
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
  
      <el-dialog :title="title" v-model="open" width="600px" append-to-body>
        <el-form ref="orderRef" :model="form" :rules="rules" label-width="80px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="用户ID" prop="userId">
                <el-input v-model="form.userId" placeholder="请输入用户ID" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="总金额" prop="totalAmount">
                <el-input v-model="form.totalAmount" type="number" placeholder="请输入总金额" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="订单状态" prop="status">
                <el-select v-model="form.status" placeholder="选择订单状态">
                  <el-option label="待处理" :value="0" />
                  <el-option label="已完成" :value="1" />
                  <el-option label="已取消" :value="2" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="支付方式" prop="paymentMethod">
                <el-select v-model="form.paymentMethod" placeholder="选择支付方式">
                  <el-option label="信用卡" :value="0" />
                  <el-option label="借记卡" :value="1" />
                  <el-option label="PayPal" :value="2" />
                  <el-option label="货到付款" :value="3" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="送货地址" prop="shippingAddress">
                <el-input v-model="form.shippingAddress" placeholder="请输入送货地址" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="物流追踪号" prop="trackingNumber">
                <el-input v-model="form.trackingNumber" placeholder="请输入物流追踪号" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="折扣码" prop="discountCode">
                <el-input v-model="form.discountCode" placeholder="请输入折扣码" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </template>
  
  <script setup name="OrderList">
  import { ref, reactive } from 'vue';
  import { listOrders, addOrder, updateOrder, deleteOrder } from "@/api/pages/order";
  
  const loading = ref(true);
  const open = ref(false);
  const showSearch = ref(true);
  const title = ref("");
  const refreshTable = ref(true);
  const orderList = ref([]);
  const form = reactive({
    id: undefined,
    userId: undefined,
    totalAmount: 0.00,
    orderDate: new Date(),
    status: 0,
    paymentMethod: 0,
    shippingAddress: "",
    trackingNumber: "",
    discountCode: ""
  });
  const queryParams = reactive({
    userId: undefined,
    status: undefined
  });
  const rules = {
    userId: [{ required: true, message: "用户ID不能为空", trigger: "blur" }],
    totalAmount: [{ required: true, message: "总金额不能为空", trigger: "blur" }],
  };
  
  // 查询订单列表
  const getList = () => {
    loading.value = true;
    listOrders(queryParams).then(response => {
      orderList.value = response.data;
      loading.value = false;
    });
  };
  
  // 取消按钮
  const cancel = () => {
    open.value = false;
    reset();
  };
  
  // 重置表单
  const reset = () => {
    Object.assign(form, {
      id: undefined,
      userId: undefined,
      totalAmount: 0.00,
      orderDate: new Date(),
      status: 0,
      paymentMethod: 0,
      shippingAddress: "",
      trackingNumber: "",
      discountCode: ""
    });
  };
  
  // 搜索按钮操作
  const handleQuery = () => {
    getList();
  };
  
  // 重置按钮操作
  const resetQuery = () => {
    Object.assign(queryParams, {
      userId: undefined,
      status: undefined
    });
    getList();
  };
  
  // 新增按钮操作
  const handleAdd = () => {
    reset();
    open.value = true;
    title.value = "新增订单";
  };
  
  // 修改按钮操作
  const handleUpdate = (row) => {
    Object.assign(form, row);
    open.value = true;
    title.value = "修改订单";
  };
  
  // 提交按钮
  const submitForm = () => {
    const apiCall = form.id ? updateOrder : addOrder;
    apiCall(form).then(() => {
      getList();
      open.value = false;
    });
  };
  
  // 删除按钮操作
  const handleDelete = (row) => {
    deleteOrder(row.id).then(() => {
      getList();
    });
  };
  
  // 获取订单状态标签
  const getOrderStatusLabel = (status) => {
    switch (status) {
      case 0: return '待处理';
      case 1: return '已完成';
      case 2: return '已取消';
      default: return '';
    }
  };
  
  // 获取支付方式标签
  const getPaymentMethodLabel = (paymentMethod) => {
    switch (paymentMethod) {
      case 0: return '信用卡';
      case 1: return '借记卡';
      case 2: return 'PayPal';
      case 3: return '货到付款';
      default: return '';
    }
  };
  
  // 初始化订单列表
  getList();
  </script>
  
  <style scoped>
  .app-container {
    padding: 20px;
  }
  .mb8 {
    margin-bottom: 8px;
  }
  .dialog-footer {
    text-align: right;
  }
  </style>